package com.huifaapp.mall.api;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonParseException;
import com.huifaapp.mall.dialogs.LoadingDialog;

import org.json.JSONException;

import java.net.ConnectException;
import java.text.ParseException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

public abstract class ObserverCallback<T> implements Observer<T> {

    //对应HTTP的状态码
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    private String TAG = "onError";
    private LoadingDialog loadingDialog;
    private Context context;
    private  boolean isShowLoading;
    /**
     *
        不显示加载Loading

     */
    public ObserverCallback() {
        this(null, false, null);
    }
    /**
     *
     * @param context 上下文
     */
    public ObserverCallback(Context context) {
        this(context, true, "加载中");
    }
    /**
     *
     * @param context 上下文
     * @param isShowLoading  是否显示加载Loading
     */

    public ObserverCallback(Context context, boolean isShowLoading) {
        this(context, isShowLoading, "加载中");
    }
    /**
     *
     * @param context 上下文
     * @param loadingText  提示信息
     */
    public ObserverCallback(Context context, String loadingText) {
        this(context, true, loadingText);
    }

    /**
     *
     * @param context 上下文
     * @param isShowLoading  是否显示加载Loading
     * @param loadingText  提示信息
     */
    public ObserverCallback(Context context, boolean isShowLoading, String loadingText) {
        this.context = context;
        this.isShowLoading = isShowLoading;
        if (null != context) {
            loadingDialog = new LoadingDialog(context);
            loadingDialog.setContent(loadingText);


        }
    }

    @Override
    public void onSubscribe(Disposable d) {
//        d.dispose();
            if (isShowLoading){
                loadingDialog.show();
            }
    }

    @Override
    public void onNext(T value) {
        onSuccessful(value);
        if (loadingDialog != null&&loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog.cancel();
        }
    }

    @Override
    public void onError(Throwable e) {
        String errcode,errMsg;
        Log.e(TAG, e.toString());
        if (e instanceof ApiException) { //自定义异常
            ApiException exception = (ApiException) e;
            onError(exception.getCode(), TextUtils.isEmpty(exception.getMessage()) ? "" : exception.getMessage());
            errcode = exception.getCode();
            errMsg = exception.getMessage();
        } else if (e instanceof HttpException) {             //HTTP错误

            HttpException httpException = (HttpException) e;
            switch (httpException.code()) {
                case UNAUTHORIZED:
                case FORBIDDEN:
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
//                    ex.message = "网络错误";  //均视为网络错误
                    onError("", "网络错误");
                    errcode = "";
                    errMsg = "网络错误";
                    break;

            }
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
//            ex.message = "解析错误";            //均视为解析错误
            onError("", "解析错误");
            errcode = "";
            errMsg = "解析错误";
        } else if (e instanceof ConnectException) {
//            ex.message = "连接失败";  //均视为网络错误
            onError("", "连接失败");
            errcode = "";
            errMsg = "连接失败";
//            ToastUtils.showLongToast("连接失败");
        } else {
//            ex.message = "未知错误";          //未知错误
            onError("", "未知错误");
            errcode = "";
            errMsg = "系统错误";
        }

        Log.e(TAG,"errcode>>"+errcode+"||||"+"errMsg>>"+errMsg);
        ToastUtils.showShortSafe(errMsg);
        if (loadingDialog != null&&loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog.cancel();
        }
    }

    @Override
    public void onComplete() {
        if (loadingDialog != null&&loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog.cancel();
        }
    }


    protected abstract void onSuccessful(T resp);

    protected abstract void onError(String code, String errMsg);
}
